//
//  HomeViewController.m
//  GiftNotes
//
//  Created by wsh on 15/8/11.
//  Copyright (c) 2015年 wsh. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeMenueView.h"
#import "MainBaseViewController.h"
#import "HomeCommonData.h"
#import "HomeBaseViewController.h"
#import "HomeSelectionViewController.h"
#import "HomeHeader.h"
#import "Screen.h"

@interface HomeViewController ()<HomeMenuDelegate>

@property (nonatomic, strong) UIBarButtonItem *leftBarButtonItem;
@property (nonatomic, strong) HomeMenueView *menu;  //  菜单按钮
@property (nonatomic, strong) UIButton *button;  // 标题按钮

@property (nonatomic, strong) HomeBaseViewController *baseVC;  //  除了(精品界面)的其他界面的controller
@property (nonatomic, strong) HomeSelectionViewController *selectionVC;  //  精品界面的controller

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];

    //  改变下一层返回按钮的标题
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:backItem];
    self.navigationController.navigationBar.backgroundColor = [UIColor orangeColor];
    
    [self creatMenue];
    
    //  除了(精品界面)的其他界面的controller的实例化
    _baseVC = [[HomeBaseViewController alloc]init];
    _baseVC.naVC = self.navigationController;
    
    
    //  精品界面的controller的实例化
    _selectionVC = [[HomeSelectionViewController alloc]init];
    _selectionVC.naVC = self.navigationController;
    
    [self creatSelectionView];
}



//  实现点击下一层界面能够收起
- (void)viewWillDisappear:(BOOL)animated
{
    [self.menu menuDismiss];
}


#pragma mark --- 创建精品页面
- (void)creatSelectionView{
    
    _selectionVC.view.frame = CGRectMake(0, 64, kHomeWidth, kHomeHeight - 64);
    [self.view addSubview:_selectionVC.view];
}


#pragma mark --- 创建下拉菜单
- (void)creatMenue{
    
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button setTitle:@"精选" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(doTapLeftBarButton:) forControlEvents:UIControlEventTouchUpInside];
    [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    self.navigationItem.titleView = _button;
    
    NSArray *array = @[@"精选", @"礼物", @"美食", @"数码", @"运动", @"娱乐"];
    _menu = [[HomeMenueView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 80, 64, 70, 264) array:array];
    _menu.delegate = self;
}


#pragma mark ---  action
//  button的点击事件
- (void)doTapLeftBarButton:(UIBarButtonItem *)sender{
    
    if (_menu.isShow) {
        
        [self.menu menuDismiss];
    } else {
        
        [self.menu menuShow:self.navigationController];
    }
}


//  实现HomeMenueView类的代理方法(实现界面的跳转)
- (void)transportMenuName:(NSString *)name index:(NSInteger)index{
    
    [_button setTitle:name forState:UIControlStateNormal];
    
    [_baseVC.view removeFromSuperview];
    [_selectionVC.view removeFromSuperview];
    
    
    if (index == 0) {
        //  精选界面
        
        [self creatSelectionView];
        
    } else {
        //  其他界面
        
        HomeCommonData *data = [HomeCommonData defaultData];
        NSString *url = data.classArray[index - 1];
        _baseVC.url = url;
        [self.view addSubview:_baseVC.view];
        
        _baseVC.view.frame = CGRectMake(0, 64, kHomeWidth, kHomeHeight );
    }
}




@end
